<?php

require_once("view/AdministrationView.php");

class AdministrationController{
	
	private $administrationView;
	
	public function __construct(){
		$this->administrationView = new AdministrationView();
	}
	
	public function doControll($a_userHandler, $a_userFileController){
		$page = new Page();
		$signUpController = new SignUpController($a_userHandler);
		
		//Handle input
		//if Admin wants to change a password
		if($this->administrationView->didAdminChangePasswordOnUser()){
			$userIdToChange = $this->administrationView->getUserIdToChangePassword();
			$userToChange = $a_userHandler->getUserByuserId($userIdToChange);
			if($userIdToChange !== null){
				$userToChange = $a_userHandler->updatePasswordOnUser($userToChange);
			}
			$this->administrationView->generateChangePassConfirm($userToChange);
		}
		
		//if Admin wants to remove a user
		if ($this -> administrationView -> didAdminTryToRemoveUser() === true) {
			$userIdToRemove = $this -> administrationView -> getUserIdToRemove();
			$userToRemove = $a_userHandler -> getUserByuserId($userIdToRemove);

			//Remove user
			if ($userToRemove !== null) {
				$result = $a_userHandler -> deleteUser($userToRemove);
			} 
			else {
				$result = false;
			}

			if ($result === true) {
				//Remove users Files
				$a_userFileController -> DoControll(null, null, $userIdToRemove);
				//Set confirm message
				
			}
			$this -> administrationView -> generateRemoveUserConfirm($userToRemove);
		}

		//Handle output
		$allUsers = $a_userHandler -> getAllUsers();
 		$page -> body = $this -> administrationView -> generateAdminView($allUsers);
		$signUpPage = $signUpController->doControll(true);
		$page->body .= $signUpPage->body;
		$page -> title = " | Admin";
		return $page;
	}
}
		